﻿using System;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace TheWorldLibrary.Controls
{
    public enum State { Normal, Hover, Clicked }

    public class Button : Control
    {
        #region Field Region

        PictureBox buttonNormal, buttonHover, buttonClicked;
        State bState;
        int pRegionX_left, pRegionX_right, pregionY_top, pRegionY_bot;


        #endregion

        #region Property Region

        #endregion

        #region Constructors

        public Button(Texture2D _buttonTexture, Texture2D _hButtonTexture, Texture2D _CButtonTexture, Vector2 _position, bool Enabled)
        {
            this.Enabled = Enabled;
            int oneButton = _buttonTexture.Width / 4;
            pRegionX_left = (int)Position.X;
            pRegionX_right = (int)Position.X + oneButton;
            pRegionY_bot = (int)Position.Y;
            pregionY_top = (int)Position.Y + _buttonTexture.Height;
            Bounds = new Rectangle((int)_position.X, (int)_position.Y, _buttonTexture.Width, _buttonTexture.Height);

            // Proprieties
            buttonClicked = new PictureBox(_CButtonTexture,
                new Rectangle((int)_position.X, (int)_position.Y, _buttonTexture.Width, _buttonTexture.Height),
                new Rectangle(0, 0, _buttonTexture.Width, _buttonTexture.Height));

            buttonNormal = new PictureBox(_buttonTexture,
                new Rectangle((int)_position.X, (int)_position.Y, (_buttonTexture.Width), _buttonTexture.Height),
                new Rectangle(0, 0, _buttonTexture.Width, _buttonTexture.Height));

            buttonHover = new PictureBox(_hButtonTexture,
                new Rectangle((int)_position.X, (int)_position.Y, _buttonTexture.Width, _buttonTexture.Height),
                new Rectangle(0, 0, _buttonTexture.Width, _buttonTexture.Height));

            bState = State.Normal;
        }

        #endregion

        #region Abstract Method Region

        public override void Update(GameTime gameTime)
        {
            if (!Enabled)
            {
                Enabled = true;
                return;
            }
            if (ButtonState.Pressed == Mouse.GetState().LeftButton && Bounds.Contains(Mouse.GetState().X, Mouse.GetState().Y))
            {
                bState = State.Clicked;
                OnClicked(null);
            }
            else if (Bounds.Contains(Mouse.GetState().X, Mouse.GetState().Y))
            {
                bState = State.Hover;
            }
            else
                bState = State.Normal;
        }

        public override void HandleInput(PlayerIndex playerIndex)
        {

        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            if (!Enabled) return;

            switch (bState)
            {
                case State.Normal:
                    buttonNormal.Draw(spriteBatch);
                    break;
                case State.Hover:
                    buttonHover.Draw(spriteBatch);
                    break;
                case State.Clicked:
                    buttonClicked.Draw(spriteBatch);
                    break;

            }
        }

        public event EventHandler Clicked;

        protected virtual void OnClicked(EventArgs e)
        {
            if (Clicked != null)
            {
                Clicked(this, e);
            }
        }

        #endregion
    }
}
